﻿using QFramework;
using SoulCard.Models;
using System;
using System.Collections.Generic;
using System.Json;

namespace SoulCard
{
    public class GetAllCards
    {
        private const string URL = "http://liangxiegame.com/api/cards/";
        
        public static List<CardData> DoGetAllCard(string token)
        {
            var responseText = HttpUtil.Get("http://liangxiegame.com/api/cards/", new Dictionary<string, string>{
                {"Authorization", "Token " + token}
            });


            Console.WriteLine(responseText);

            JsonValue cardJsonArray = JsonValue.Parse(responseText);
            var cardDatas = new List<CardData>();

            for (int i = 0; i < cardJsonArray.Count;i++)
            {
                var cardJsonData = cardJsonArray[i];
                int id = cardJsonData["id"];
                string content = cardJsonData["content"];
                bool isCompleted = cardJsonData["is_completed"];
                bool isSetTop = cardJsonData["is_set_top"];
                string createdTime = cardJsonData["created_time"];
                string modifiedAt = cardJsonData["modified_at"];

                cardDatas.Add(new CardData(id.ToString())
                {
                    Content = content,
                    Complete = isCompleted,
                    SetTop = isSetTop,
                    CreatedAt = createdTime,
                    ModifiedAt = modifiedAt
                });
            }


            return cardDatas;
        }
    }
}